#ifndef COMMON_TASK_QUEUE_H_
#define COMMON_TASK_QUEUE_H_
#include <queue>
#include "simplelock.h"
#include "yysemaphore.h"

namespace thread {

/// forward declaration
class Task;

/*! \class TaskQueue
    \brief A thread safe Task Queue for multiple thread environment
 */
class TaskQueue
{
public:

  TaskQueue(
    uint32_t queue_threshold = 30000,
    uint32_t queue_max_limit = 35000);

  ~TaskQueue();

  /**
   * Try to pop one task from the queue, if the queue is empty,
   * if queue is empty, NULL will be return
   * @return     task pointer
   */
  Task* Pop();

  /**
   * Push a task to queue
   * @param tk  task pointer
   * @return true to indicate successful, false to fail
   */
  bool Push(Task* tk);

  inline bool Empty() const
  {
    return tasks_.empty();
  }

  uint32_t Size();
 
private:

  Semaphore         semaphore_;
  SimpleLock        lock_;
  std::queue<Task*> tasks_;
  uint32_t          queue_threshold_;
  uint32_t          queue_max_limit_;

}; // TaskQueue

}

#endif // YY_COMMON_TASK_QUEUE_H_

